//获取应用实例
var app = getApp()
var utils = require('../../utils/utils.js')
Page({
    data: {
        addIcon:utils.sysConst.addIcon,
        soleOVerIcon:utils.sysConst.soleOVerIcon,
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false
    },
    //初始化
    onLoad: function() {
        var that = this
        var param =null;
        //滚动条信息
        wx.request({
            url: utils.sysConst.sliderListPath,
            method: 'GET',
            data: param,
            header: {
                'Accept': 'application/json','appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            success: function(res) {
                if (res.data.code == 1){
                    that.setData({
                        images: res.data.data
                    })
                }
            }
        })
        //店长推荐产品列表
        wx.request({
            url: utils.sysConst.recommendPath,
            method: 'GET',
            data: param,
            header: {
                'Accept': 'application/json','appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            success: function(res) {
                if (res.data.code == 1){
                    that.setData({
                        venuesItems: res.data.data,
                    })
                    setTimeout(function () {
                        that.setData({
                            loadingHidden: true
                        })
                    }, 1500)
                }
            }
        })
        // 获取用户信息 加入购物车时需要用
        wx.login({
            success: function (res) {
                var param=  {"code":res.code,"appId":app.globalData.appId}
                //调用后台接口
                wx.request({
                    url: utils.sysConst.getUserInfoPath,
                    method: 'POST',
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
                    },
                    data: param,
                    dataType: "json",
                    success: function(res) {
                        if (res.data.code == 1){
                            //设置全局用户信息-openid
                            app.globalData.openid = res.data.data.openid
                            //console.log("app.globalData.openid-index",app.globalData.openid)
                        }else{
                            wx.showToast({
                                title:"获取用户信息失败："+res.data.msg,
                                icon: 'none',
                                duration: 2000
                            })
                        }
                    }
                })
                // == getUserInfo end
            }
        });
        //wx.login === end ===
    },
    //加入购物车-start
    addChartFromIndex:function(event){
        wx.showLoading({
            title: '处理中...',
        })
        var that = this
        var param = {"productId":event.currentTarget.dataset.id,"openId":app.globalData.openid}
        wx.request({
            url: utils.sysConst.addCartPath,
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
            },
            data: param ,
            dataType: "json",
            success: function(res) {
                wx.hideLoading();
                // console.log("addCart-back====",res.data)
                if (res.data.code == 1){
                    wx.showToast({
                        title: '添加成功',
                        icon: 'success',
                        duration: 2000
                    })
                }else{
                    wx.showToast({
                        title:"加入失败："+res.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    }
    //加入购物车-end
})
